###############################################################################
#
# CMake build scripts for BlipfotoUploader
# 
# Created 2012/01/11 by Peter Bunting
#
# These scripts were initial based on those used for SPDPointsViewer
#
# History
# 2012/01/11 - Created by Peter Bunting
#
###############################################################################

###############################################################################
# Set Project name and version
project (BlipUp)

set (PROJECT_SOURCE_DIR src)

# The version number.
set (BLIPUP_VERSION_MAJOR 0)
set (BLIPUP_VERSION_MINOR 0)
set (BLIPUP_VERSION_PATCH 1)
set (BLIPUP_VERSION "${BLIPUP_VERSION_MAJOR}.${BLIPUP_VERSION_MINOR}.${BLIPUP_VERSION_PATCH}")
set (BLIPUP_PACKAGE_VERSION "${BLIPUP_VERSION_MAJOR}.${BLIPUP_VERSION_MINOR}.${BLIPUP_VERSION_PATCH}")
set (BLIPUP_PACKAGE_STRING "BLIPUP ${BLIPUP_VERSION_MAJOR}.${BLIPUP_VERSION_MINOR}.${BLIPUP_VERSION_PATCH}")
set (BLIPUP_PACKAGE_BUGREPORT "petebunting@mac.com")
set (BLIPUP_PACKAGE "BlipUp")

set(BLIPUP_APP BlipUp)
###############################################################################

###############################################################################
# CMake settings
cmake_minimum_required(VERSION 2.6.0)

IF(NOT CMAKE_BUILD_TYPE)
  #SET(CMAKE_BUILD_TYPE "DEBUG")
  SET(CMAKE_BUILD_TYPE "RELEASE")
  #SET(CMAKE_BUILD_TYPE "RELWITHDEBINFO")
  #SET(CMAKE_BUILD_TYPE "MINSIZEREL")
ENDIF()

set(CMAKE_COLOR_MAKEFILE ON)

# Allow advanced users to generate Makefiles printing detailed commands
mark_as_advanced(CMAKE_VERBOSE_MAKEFILE)

# Path to additional CMake modules
set(CMAKE_MODULE_PATH ./cmake/modules ${CMAKE_MODULE_PATH})
###############################################################################

###############################################################################
# Documentation
file(READ "doc/index.txt" README )
file(WRITE "README.txt" "${README}")
###############################################################################

###############################################################################
# Platform and compiler specific settings

if(WIN32)
    if (MSVC)
        if (MSVC80 OR MSVC90 OR MSVC10)
            add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
            add_definitions(-D_CRT_SECURE_NO_WARNINGS)
            add_definitions(-D_CRT_NONSTDC_NO_WARNING)
            add_definitions(-D_SCL_SECURE_NO_WARNINGS)
        endif()
        
        if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
            string(REGEX REPLACE "/W[0-4]" "/W4"
                   CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
        else()
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
        endif()

        # Generate dot-user file with user-specific settings for Visual Studio project
        set(MSVC_ENVIRONMENT_PATH "" CACHE STRING
            "Custom PATH for Environment property in Visual Studio project configuration")
        mark_as_advanced(MSVC_ENVIRONMENT_PATH)
        set(VCPROJ_USER_ENVIRONMENT_PATH_DEBUG "${ENVIRONMENT_PATH}")
        mark_as_advanced(VCPROJ_USER_ENVIRONMENT_PATH_DEBUG)

    endif()

else()
  # Recommended C++ compilation flags
  # -Weffc++
  set(BLIPUP_COMMON_CXX_FLAGS
	"-pipe -ansi -Wall -Wpointer-arith -Wcast-align -Wcast-qual -Wfloat-equal -Wredundant-decls -Wno-long-long")
	#"-pedantic -ansi -Wall -Wpointer-arith -Wcast-align -Wcast-qual -Wfloat-equal -Wredundant-decls -Wno-long-long")

  if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC ${BLIPUP_COMMON_CXX_FLAGS}")
    if (CMAKE_COMPILER_IS_GNUCXX)
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++98")
    endif()

  elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" OR "${CMAKE_CXX_COMPILER}" MATCHES "clang")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${BLIPUP_COMMON_CXX_FLAGS}")
  endif()
endif(WIN32)

IF( APPLE )
    SET( PROGNAME BlipUp )
	SET( MACOSX_BUNDLE_BUNDLE_NAME BlipUp)
	SET( MACOSX_BUNDLE_INFO_STRING "Blipfoto Uploader")
    SET( MACOSX_BUNDLE_ICON_FILE BlipUp.icns )
    SET( MACOSX_BUNDLE_SHORT_VERSION_STRING 0.01-Alpha )
    SET( MACOSX_BUNDLE_BUNDLE_VERSION 0.01-Alpha )
    SET( MACOSX_BUNDLE_LONG_VERSION_STRING Version 0.01-Alpha )
    SET( CMAKE_OSX_ARCHITECTURES x86_64 ) #Comment out if not universal binary
	SET( MACOSX_BUNDLE_COPYRIGHT "Copyright Pete Bunting")
	SET( MACOSX_BUNDLE_GUI_IDENTIFIER "com.mac.blipup") 
  ELSE( APPLE )
    SET( PROGNAME BlipUp )
  ENDIF( APPLE )
###############################################################################

###############################################################################
# Check the required libraries are present

FIND_PACKAGE( Qt4 COMPONENTS QtCore QtGUI QtNetwork QtXML REQUIRED )

SET(QT_USE_QTNETWORK true)
SET(QT_USE_QTXML true)

if (QT4_FOUND)
	include_directories (${QT4_INCLUDE_DIR} ${QT_QTNETWORK_INCLUDE_DIR} ${QT_QTXML_INCLUDE_DIR})
endif(QT4_FOUND)

INCLUDE( ${QT_USE_FILE} )

# WARNING - HACK! File paths for libEXIF have been hardcoded!
set(EXIF_INCLUDE_DIR /usr/local/include/)
include_directories (${EXIF_INCLUDE_DIR})
set(EXIF_LIBRARY -L/usr/local/lib/)
set(EXIF_LIBRARY ${EXIF_LIBRARY} -lexif)

###############################################################################

###############################################################################
# Build Application

SET( BLIPUP_SRCS
	./src/main.cpp 
	./src/BlipUpException.cpp 
	./src/BlipUpInitApplication.cpp
	./src/BlipUpInterface.cpp
)

SET( BLIPUP_HDRS
	./src/BlipUpException.h
    ./src/BlipUpInitApplication.h
	./src/BlipUpInterface.h
)

SET( BLIPUP_MOC_HDRS
	./src/BlipUpInitApplication.h 
	./src/BlipUpInterface.h
)

QT4_WRAP_CPP( BLIPUP_MOC_SRCS ${BLIPUP_MOC_HDRS} )

INCLUDE_DIRECTORIES( ${CMAKE_BINARY_DIR} )

IF( APPLE )
  ADD_EXECUTABLE( ${PROGNAME} MACOSX_BUNDLE ${BLIPUP_SRCS} ${BLIPUP_HDRS} ${BLIPUP_MOC_SRCS} )
#  ADD_CUSTOM_COMMAND( TARGET ${PROGNAME} POST_BUILD
#    COMMAND mkdir ARGS ${CMAKE_CURRENT_BINARY_DIR}/Debug/${PROGNAME}.app/Contents/Resources
#    COMMAND cp ARGS ${MACOSX_BUNDLE_ICON_FILE} ${CMAKE_CURRENT_BINARY_DIR}/Debug/${PROGNAME}.app/Contents/Resources
#    COMMAND cp ARGS ${MACOSX_FILE_ICON_FILE} ${CMAKE_CURRENT_BINARY_DIR}/Debug/${PROGNAME}.app/Contents/Resources )
ELSE( APPLE )
  ADD_EXECUTABLE( ${PROGNAME} ${BLIPUP_SRCS} ${BLIPUP_HDRS} ${BLIPUP_MOC_SRCS} )
ENDIF( APPLE )

TARGET_LINK_LIBRARIES( ${PROGNAME} ${QT_LIBRARIES} ${EXIF_LIBRARY})

###############################################################################

###############################################################################
# Create Distribution
SET(CPACK_SOURCE_GENERATOR "TGZ;ZIP;TBZ2")
SET(CPACK_CMAKE_GENERATOR "Unix Makefiles")

set(CPACK_SOURCE_PACKAGE_FILE_NAME
  "${CMAKE_PROJECT_NAME}-${BLIPUP_VERSION_MAJOR}.${BLIPUP_VERSION_MINOR}.${BLIPUP_VERSION_PATCH}")

# Set files to ignore
list(APPEND CPACK_SOURCE_IGNORE_FILES "_CPack_Packages")
list(APPEND CPACK_SOURCE_IGNORE_FILES ".gz")
list(APPEND CPACK_SOURCE_IGNORE_FILES ".bz2")
list(APPEND CPACK_SOURCE_IGNORE_FILES ".zip")
list(APPEND CPACK_SOURCE_IGNORE_FILES ".svn")
list(APPEND CPACK_SOURCE_IGNORE_FILES "README")
list(APPEND CPACK_SOURCE_IGNORE_FILES "HOWTORELEASE.txt")
list(APPEND CPACK_SOURCE_IGNORE_FILES "CMakeCache.txt")
list(APPEND CPACK_SOURCE_IGNORE_FILES "CPackConfig.cmake")
list(APPEND CPACK_SOURCE_IGNORE_FILES "schemas")

include(CPack)

add_custom_target(dist COMMAND ${CMAKE_MAKE_PROGRAM} package_source)
###############################################################################
